package com.demo2do.boyle.client.web.controller.cpc;

import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demo2do.boyle.client.service.CpcConsumeService;
import com.demo2do.boyle.client.service.CpcOpenidService;
import com.demo2do.boyle.client.service.CpcPvService;
import com.demo2do.boyle.client.service.CpcService;
import com.demo2do.boyle.common.entity.cpc.Cpc;
import com.demo2do.boyle.common.entity.cpc.CpcConsume;
import com.demo2do.boyle.common.entity.cpc.CpcOpenid;
import com.demo2do.boyle.common.entity.cpc.CpcPv;
import com.demo2do.core.persistence.GenericDaoSupport;

@Controller
@RequestMapping("/interface")
public class CpcController {
	
	@Autowired
	private CpcService cpcService;
	
	@Autowired
	private CpcPvService cpcPvService;
	
	@Autowired
	private CpcOpenidService cpcOpenidService;
	
	@Autowired
	private CpcConsumeService cpcConsumeService;
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@RequestMapping("/cpc")
	public void cpc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		request.setCharacterEncoding("gb2312");
		response.setCharacterEncoding("gb2312");
		// 设置响应内容类型
		response.setContentType("text/html");
		
		//1。获取 openid 
		@SuppressWarnings("unchecked")
		Map<String,Object> map = cpcService.getOpenidFromWinxin(request,response);
		String openid = map.get("openid").toString();
		String source = map.get("source").toString();
		String cpcid = map.get("cpcid").toString();
		//String openid = "oT7sFuHSIqe9B1IwhwyGBc7P2WHg";
		System.out.println("|||||||||  openid==>"+openid);
		
		//load Cpc 表数据
		Cpc cpc = cpcService.load(Cpc.class, cpcid);
		Integer fee = cpc.getFee();
		Integer price = cpc.getPrice();
		
		//2. 往表 cpc_openid 插入数据
		String sentence = " FROM CpcOpenid WHERE CpcOpenid.openid = :openid ";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("openid", openid);
		
		List<Map<String,Object>> cpcOpenList = genericDaoSupport.queryForList(sentence, parameters);
		CpcOpenid cpcOpenid = cpcOpenidService.load(CpcOpenid.class, cpcOpenList.get(0).get("id").toString());
		if(cpcOpenList.size() > 0){
			//已有该 openid : update
			cpcOpenid.setAmount(price);
			cpcOpenid.setDepositPoint(cpcOpenid.getDepositPoint() + price);
			cpcOpenidService.update(cpcOpenid);
			
		}else{
			//insert
			CpcOpenid CpcOpenid = new CpcOpenid(openid, price);
			cpcOpenidService.save(CpcOpenid);
		}
		
		//3. 往 cpc_consume 表增加一条消费记录
		CpcConsume cpcConsume = new CpcConsume(Integer.valueOf(cpcid),Integer.valueOf(openid),Integer.valueOf(source),fee,price);
		cpcConsumeService.save(cpcConsume);
		
		//操作者浏览器类型
		String agent = request.getHeaders("User-Agent").toString();
		String useragent = cpcPvService.getBrowserName(agent);
		
		//服务器IP地址
		String ip=InetAddress.getLocalHost().getHostAddress();
		
		//4. 插入 pv表(不管请求是否合格)，cpc pv字段加1
		CpcPv cpcPv = new CpcPv(Integer.valueOf(cpcid), Integer.valueOf(openid), Integer.valueOf(source), useragent, ip);
		cpcPvService.save(cpcPv);
		
	}
	
}
